package com.megvii.corelib.base;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.corelib.components.NetworkErrorView;
import com.megvii.corelib.components.ProgressBarView;

import java.io.Serializable;

import butterknife.ButterKnife;

/**
 * Created by linyue on 15/12/22.
 */
public abstract class MegviiFragment<PRESENTER extends MegviiPresenter> extends Fragment
        implements PresenterHolder<PRESENTER>, NetworkErrorView.ButtonListener
{
    protected ProgressBarView progressBar;

    protected PRESENTER presenter;

    protected BaseNetworkErrorView networkErrorView;

    protected boolean created = false;

    protected boolean networkError = false;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        if (autoButterKnifeBind())
        {
            ButterKnife.bind(this, view);
        }
        buildProgressBar();
        buildNetworkErrorLayout();
        if (networkError)
        {
            presenter.onClickRefreshNetwork();
        }
    }

    /**
     * 如果子类不要将views绑定到自己中，则重载这个函数，返回false
     *
     * @return
     */
    protected boolean autoButterKnifeBind()
    {
        return true;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        if (presenter == null)
        {
            presenter = MegviiPresenter.buildPresenter(getPresenterClass());
            presenter.doInit(this);
        }
    }

    @Override
    public void onStart()
    {
        super.onStart();
        if (!created)
        {
            presenter.onCreated();
            created = true;
        }
        presenter.onStart();
    }

    @Override
    public void onStop()
    {
        super.onStop();
        presenter.onStop();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        presenter.onResume();
    }

    @Override
    public void onHiddenChanged(boolean hidden)
    {
        super.onHiddenChanged(hidden);
        if (hidden)
        {
            presenter.onPause();
        }
        else
        {
            presenter.onResume();
        }
    }

    private void buildProgressBar()
    {
        if (progressBar == null)
        {
            progressBar = new ProgressBarView(this.getActivity());
            //将progress bar加到了最上层content view同级的位置
            View root = getActivity().findViewById(android.R.id.content);
            if (root instanceof ViewGroup)
            {
                ((ViewGroup) root).addView(progressBar);
            }
        }
    }

    private void buildNetworkErrorLayout()
    {
        networkErrorView = getNetworkErrorView();
        if (networkErrorView != null)
        {
            networkErrorView.setButtonListener(this);
        }
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        this.presenter.onDestory();
        if (autoButterKnifeBind())
        {
            ButterKnife.unbind(this);
        }
    }

    @Override
    public void onPause()
    {
        super.onPause();
        this.presenter.onPause();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    public void startActivity(Class<?> destinationClass, Serializable destinationParam)
    {
        Intent intent = new Intent(getActivity(), destinationClass);
        if (destinationParam != null)
        {
            intent.putExtra(MegviiActivity.KEY_ACTIVITY_PARAM, destinationParam);
        }
        startActivity(intent);
    }

    @Override
    public void showProgressBar()
    {
        progressBar.show();
    }

    @Override
    public void hideProgressBar()
    {
        progressBar.hide();
    }

    @Override
    public Context getContextX()
    {
        return getActivity();
    }

    @Override
    public void showNetworkError()
    {
        networkError = true;
        if (networkErrorView != null)
        {
            networkErrorView.show();
        }
    }

    @Override
    public void hideNetworkError()
    {
        networkError = false;
        if (networkErrorView != null)
        {
            networkErrorView.hide();
        }
    }

    @Override
    public void onNetworkErrorButtonClick()
    {
        presenter.onClickRefreshNetwork();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        return false;
    }
}
